package StuManSysPro;
import StudentMangeSystem.Student;
import StudentMangeSystem.StudentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StuSysPro {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码 4退出");
            String choose=sc.next();
            switch (choose) {
                case "1"-> login(list);
                case "2"-> register(list);
                case "3"-> forgetPassword(list);
                case "4"-> {
                    System.out.println("谢谢使用，再见");
                    System.exit(0);}
                default -> System.out.println("没有这个选项");
            }
        }

    }

    //登录
    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        //判断用户名是否存在
        boolean flag=contains(list,username);
        if(!flag) {
            System.out.println("用户名"+username+"请先注册，再登陆");
            return;
        }
        System.out.println("请输入密码");
        String password = sc.next();
        while (true) {
            String rightCode=getCode();
            System.out.println("当前正确验证码为"+rightCode);
            System.out.println("请输入验证码");
            String code=sc.next();
            if (code.equalsIgnoreCase(rightCode)){
                System.out.println("验证码正确");
                break;
            }else {
                System.out.println("验证码错误");
                continue;
            }
        }

        User useInfo=new User(username,password,null,null);
        boolean result=checkUserInfo(list,useInfo);
        if (result){
            System.out.println("登陆成功");
            StudentSystem ss=new StudentSystem();//创建对象调用方法，启动学生管理系统
            ss.startStudentSystem();
        }else {
            System.out.println("登陆失败，账号或密码错误");
        }


    }

    private static boolean checkUserInfo(ArrayList<User> list,User useInfo) {
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            if (user.getUsername().equals(useInfo.getUsername())&&user.getPassword().equals(useInfo.getPassword())) {
                return true;
            }
        }
        return false;
    }

    //登录生成验证码
    private static String getCode(){
        ArrayList<Character> list = new ArrayList<>();//创建集合添加所有大小写字母
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        Random r = new Random();//随机抽四个
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char ch = list.get(index);
            sb.append(ch);
        }
        int number=r.nextInt(10);//随机数字添加到末尾
        sb.append(number);
        char[] arr = sb.toString().toCharArray();//要修改字符串内容，先变字符串为字符数组，在数组中和修改，在创建一个新的字符串
        int randomIndex = r.nextInt(arr.length);//拿着最后一个索引与随即索引交换
        char temp=arr[randomIndex];//最大索引指向的元素 跟随即索引指向的元素交换
        arr[randomIndex]=arr[arr.length-1];
        arr[arr.length-1]=temp;
        return new String(arr);
    }

    //注册
    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        //录入用户名
        String username;
        while (true) {
            System.out.println("请输入用户名");
            username=sc.next();
            boolean flag1=checkUsername(username);
            if(!flag1){
                System.out.println("格式错误，重新输入");
                continue;
            }
            //校验唯一
            boolean flag2=contains(list,username);
            if(flag2){
                System.out.println("用户名"+username+"已存在，请重新输入");
            }else {
                System.out.println("用户名"+username+"可使用");
                break;
            }

        }

        //录入密码
        String password;
        while (true) {
            System.out.println("请输入要注册的密码");
             password=sc.next();
            System.out.println("再次输入要注册的密码");
            String againPassword=sc.next();
            if(!password.equals(againPassword)){
                System.out.println("两次密码不一致请重新输入");
                continue;
            }else {
                System.out.println("两次密码一致，继续录入其他数据");
                break;
            }
        }

        //录入身份证号码
        String personID;
        while (true) {
            System.out.println("请输入身份证号码");
            personID=sc.next();
            boolean flag=checkPersonID(personID);
            if (flag){
                System.out.println("身份证号码正确");
                break;
            }else{
                System.out.println("身份证号码格式错误请重新输入");
                continue;
            }
        }

        //录入手机号
        String phoneNumber;
        while (true) {
            System.out.println("请输入手机号码");
            phoneNumber=sc.next();
            boolean flag=checkPhoneNumber(phoneNumber);
            if (flag){
                System.out.println("手机号码正确");
                break;
            }else{
                System.out.println("手机号码格式错误请重新输入");
                continue;
            }
        }

        //把信息放到对象中
        User u=new User(username,password,personID,phoneNumber);
        //用户添加到集合中
        list.add(u);
        System.out.println("注册成功");
        //遍历集合
        printList(list);


    }

    //注册遍历集合方便查看信息
    private static void printList(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            System.out.println(user.getUsername()+", "+user.getPassword()+", "
                    +user.getPersonID()+", "+user.getPhoneNumber());

        }
    }

    //注册录入手机号验证条件判断
    private static boolean checkPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {//长度为11位
            return false;
        }
        if (phoneNumber.startsWith("0")){//不能以0为开头
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if(!(c>='0'&&c<='9')){
                return false;
            }
        }
        return true;
    }

    //注册录入身份证号码条件判断
    private static boolean checkPersonID(String personID) {
        if(personID.length() !=18){//长18位
            return false;
        }
        if(personID.startsWith("0")){//不能以0为开头
            return false;
        }
        for(int i=0;i<personID.length()-1;i++){//前17位，必须都是数字
            char c = personID.charAt(i);
            if(!(c>='0'&&c<='9')){
                return false;
            }
        }
        char endChar=personID.charAt(personID.length()-1);//最为一位可以是数字，也可以是大写X或小写x
        if((endChar>='0'&&endChar<='9')||(endChar=='x')||(endChar=='X')){
            return true;
        }else {
            return false;
        }
    }

    //注册录入用户名校验唯一
    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            String rightUsername=user.getUsername();
            if (rightUsername.equals(username)) {
                return true;
            }
        }
        return false;
    }

    //注册录入用户名,内容校验,3-15位之间,只能是字母加数字的组合，但是不能是纯数字
    private static boolean checkUsername(String username) {
        int len=username.length();
        if(len<3 || len>15){//校验用户名再3-15位之间
            return false;
        }
        //内容校验
        for (int i = 0; i < username.length(); i++) {
            char c=username.charAt(i);
            if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))){
                return false;
            }
        }
        int count=0;
        for (int i = 0; i < username.length(); i++) {
            char c=username.charAt(i);
            if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                count++;
                break;
            }
        }
        return count >0;
    }

    //忘记密码
    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入用户名");
        String username=sc.next();
        boolean flag=contains(list,username);
        if (!flag){
            System.out.println("当前用户"+username+"未注册，请先注册");
            return;
        }
        System.out.println("请输入身份证号");
        String personID=sc.next();
        System.out.println("请输入手机号码");
        String phoneNumber=sc.next();

        int index=findIndex(list,username);
        User user = list.get(index);

        if (!(user.getPersonID().equalsIgnoreCase(personID) && user.getPhoneNumber().equals(phoneNumber))){
            System.out.println("身份证或手机号输入错误，不能修改");
            return;
        }
        String password;
        while (true) {
            System.out.println("请输入新的密码");
             password=sc.next();
            System.out.println("请再次输入新密码");
            String againPassword=sc.next();
            if (password.equals(againPassword)){
                System.out.println("两次密码一致");
                break;
            }else {
                System.out.println("两次密码不一致，请重新输入");
                continue;
            }
        }
        user.setPassword(password);
        System.out.println("密码修改成功");

    }

    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }

}
